﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LeadExpress.Models;
using LeadExpress.Models.ViewPages;
using System.Web.Mvc;

namespace LeadExpress.Repository
{
    public class BusinessManagementRepository : BaseManagementRepository
    {
        LeadExpressEntities entities = new LeadExpressEntities();

        public BusinessManagementModel GetModel(string pageType, string roleName)
        {
            BusinessManagementModel model = new BusinessManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
            model.ListBusinessType = new Repository.BusinessTypeManagementRepository().GetAll();
            model.ListBusiness = GetAll();

            return model;
        }

        public BusinessManagementModel GetModelAdd(string pageType, string roleName)
        {
            BusinessManagementModel model = new BusinessManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
            model.ListBusinessType = new Repository.BusinessTypeManagementRepository().GetAll();
            model.ListState = new Repository.StateRepository().GetAll();

            return model;
        }

        public BusinessManagementModel GetModelEdit(string pageType, string roleName, int id)
        {
            BusinessManagementModel model = new BusinessManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
            model.ListBusinessType = new Repository.BusinessTypeManagementRepository().GetAll();
            model.ListState = new Repository.StateRepository().GetAll();

            if (id > 0)
            {
                model.BusinessInfo = GetSingle(id);
            }

            return model;
        }

        public BusinessManagementModel GetModelByUser(string pageType, string roleName)
        {
            BusinessManagementModel model = new BusinessManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
            model.ListBusinessType = new Repository.BusinessTypeManagementRepository().GetAll();
            model.ListState = new Repository.StateRepository().GetAll();

            return model;
        }

        public BusinessManagementModel GetModelDelete(string pageType, string roleName)
        {
            BusinessManagementModel model = new BusinessManagementModel();
            model.ListBusiness = GetAll();

            return model;
        }

        public List<Business> GetAll()
        {
            List<Business> list = new List<Business>();

            var items = (from c in entities.Businesses
                         orderby c.DateCreated descending
                         select c).ToList();

            foreach (var item in items)
            {
                Business model = new Business();

                model.ID = item.ID;
                model.CompanyName = item.CompanyName;
                model.ContactTitle = item.ContactTitle;
                model.Address1 = item.Address1;
                model.Address2 = item.Address2;
                model.City = item.City;
                model.State = item.State;
                model.ZipCode = item.ZipCode;
                model.Phone = item.Phone;
                model.Fax = item.Fax;
                model.EmailAddress = item.EmailAddress;
                model.Active = item.Active;
                list.Add(model);
            }

            return list;
        }

        public Business GetSingle(int id)
        {
            return (from _object in entities.Businesses where _object.ID == id select _object).FirstOrDefault();
        }

        public Business GetSingleByUser(int id)
        {
            return (from _object in entities.Businesses where _object.CreateByUserID == id select _object).FirstOrDefault();
        }

        public void Create([Bind(Exclude = "ID, DateCreated")] Business _object)
        {
            _object.DateCreated = System.DateTime.Now;
            entities.AddToBusinesses(_object);
            entities.SaveChanges();
        }

        public void Delete(int id)
        {
            var _object = (from _list in entities.Businesses where _list.ID == id select _list).First();
            entities.DeleteObject(_object);
            entities.SaveChanges();
        }

        public void Save([Bind(Exclude = "ID")] Business _object)
        {
            _object.DateCreated = System.DateTime.Now;
            entities.SaveChanges();
        }
    }
}